package branch.feature.screenrecord

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.media.projection.MediaProjectionManager
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission

// 屏幕录制
// todo: fix code for store screen record ?
// ref link:
// https://github.com/philipplackner/RecordScreen/blob/master/app/src/main/java/com/plcoding/recordscreen/MainActivity.kt
class MainActivity : ComponentActivity() {
    private val mediaProjectionManager by lazy {
        getSystemService<MediaProjectionManager>()!!
    }

    private lateinit var recordingButton: Button
    private var isServiceRunning = false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        // 授权
        AndPermission.with(this)
            .runtime()
            .permission(Permission.Group.STORAGE)
            .start()

        setContentView(com.example.mynewsapp.R.layout.activity_main_sc)

        recordingButton = findViewById(com.example.mynewsapp.R.id.recordingButton)


        // 设置按钮点击事件
        recordingButton.setOnClickListener { v ->
            if (!hasNotificationPermission() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
            } else {
                if (isServiceRunning) {
                    val intent = Intent(this, ScreenRecordService::class.java)
                    intent.action = "STOP_RECORDING"
                    startForegroundService(intent)
                    updateButtonState(false)
                } else {
                    // 启动屏幕录制
                    screenRecordLauncher.launch(mediaProjectionManager.createScreenCaptureIntent());
                }
            }
        }
    }

    private fun hasNotificationPermission(): Boolean {
        // 检查通知权限
        return ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
                PackageManager.PERMISSION_GRANTED
    }

    private fun updateButtonState(isRunning: Boolean) {
        isServiceRunning = isRunning
        if (isRunning) {
            recordingButton.text = "Stop recording"
            recordingButton.backgroundTintList =
                ContextCompat.getColorStateList(this, com.example.mynewsapp.R.color.dark_red)
        } else {
            recordingButton.text = "Start recording"
            recordingButton.backgroundTintList =
                ContextCompat.getColorStateList(
                    this,
                    com.example.mynewsapp.R.color.splash_page_color
                )
        }
    }

    // 注册权限请求
    private val permissionLauncher = registerForActivityResult<String, Boolean>(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // 权限已授予
        } else {
            // 权限被拒绝
        }
    }

    // 注册屏幕录制启动
    private val screenRecordLauncher = registerForActivityResult<Intent, ActivityResult>(
        ActivityResultContracts.StartActivityForResult()
    ) { result: ActivityResult ->
        if (result.resultCode == RESULT_OK) {
            // 屏幕录制成功启动
            updateButtonState(true)
        }
    }
}